/*
 * Copyright (C) Michael Martin & Antonio Carrozzini (TBJD)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package org.tbjd.client.customWidgets;

import java.util.HashMap;
import java.util.Map;

import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.exceptions.TBJDException;
import org.tbjd.client.images.menu.MenuImageBundle;
import org.tbjd.client.interfaces.Builder;
import org.tbjd.client.interfaces.Decisionmaker;
import org.tbjd.client.interfaces.HasMediator;
import org.tbjd.client.listeners.resourcePicker.SearchBoxKeyboardListener;
import org.tbjd.client.mediators.AbstractMediator;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
/**
 * <p>
 * A SearchBox is a tool used to search or filter
 * </p>
 * <p>
 * It has a magnifier icon on the left and a cancel button on the right
 * </p>
 * 
 * @author Michael Martin
 * @author Antonio Carrozzini
 * @version 1.0
 * 
 */
public class SearchBox extends Composite implements Decisionmaker, Builder, HasMediator {

	private AbstractMediator mediator = null;
	private HorizontalPanel searchPanel = new HorizontalPanel();
	final private TextBox searchBox = new TextBox();

	public SearchBox() {
		build();
		initWidget(searchPanel);
	}

	public void build() {
		MenuImageBundle menuImageBundle = (MenuImageBundle) GWT.create(MenuImageBundle.class);

		searchBox.setStyleName("tbjd-searchWidget_searchBox");
		searchBox.addKeyboardListener(new SearchBoxKeyboardListener());

		PushButton cancelSearchButton = new PushButton(menuImageBundle.cancel_20x20().createImage());
		cancelSearchButton.setStyleName("tbjd-searchWidget_searchBox_cancelButton");
		cancelSearchButton.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				searchBox.setText("");
				searchBox.setFocus(true);
				Map<Integer, Object> map = new HashMap<Integer, Object>();
				map.put(NotificationsConstants.FETCH_RESOURCES, searchBox.getText());
				decide(map);
			}
		});

		searchPanel.setStyleName("tbjd-searchWidget_searchPanel");
		Image magnifierIcon = menuImageBundle.search_bw_16x16().createImage();
		magnifierIcon.setStyleName("tbjd-searchWidget_searchBox_magnifierIcon");
		searchPanel.add(magnifierIcon);
		searchPanel.add(searchBox);
		searchPanel.add(cancelSearchButton);
	}

	public void decide(Object action) {
		System.out.println("SearchBox.decide(" + action + ")");
		delegate(action);
	}

	public void delegate(Object action) {
		System.out.println("SearchBox.delegate(" + action + ")");
//		Map<Integer, String> map = new HashMap<Integer, String>();
//		map.put(NotificationsConstants.SEARCH_PROJECT_NAME_CRITERIA, ((TextBox) action).getText());
		try {
			this.mediator.decide(action);
		} catch (TBJDException e) {
			e.printStackTrace();
		}
	}

	public void setMediator(AbstractMediator mediator) {
		this.mediator = mediator;
	}

	public void handle(Object action) {
	}

	public Widget getBuildedWidget() {
		return this.searchPanel;
	}

}
